package com.example.speeddating.controller;

import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.entity.Visitor;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-07-14 10:33:47
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    private VisitorService visitorService;

    @TokenCheck(roles = "user")
    @PostMapping("/add")
    public BaseResponse addVisitor(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if(jsonString.get("visitorId")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        Visitor visitor = new Visitor();
        visitor.setUserId(tokenEntity.getId());
        visitor.setVisitorId(Long.valueOf(jsonString.get("visitorId").toString()));
        if (visitor.getUserId().equals(visitor.getVisitorId())){
            return RespGenerator.returnOK("");
        }
        visitorService.addVisitor(visitor);
        return RespGenerator.returnOK("访问成功");
    }

    @TokenCheck(roles = "user")
    @PutMapping("/update")
    public BaseResponse updateVisitor(@LoginUser TokenEntity tokenEntity){
        visitorService.batchUpdateVistors(tokenEntity.getId());
        return RespGenerator.returnOK("更新成功");
    }


    /**
     * 解锁访客
     * @param tokenEntity
     * @return
     */
    @TokenCheck(roles = "user")
    @PostMapping("/unLock")
    public BaseResponse unLockVisitor(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("userId")==null||jsonString.get("id")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        Visitor visitor = new Visitor();
        visitor.setId(Long.valueOf(jsonString.get("id").toString()));
        visitor.setVisitorId(tokenEntity.getId());
        visitor.setUserId(Long.valueOf(jsonString.get("userId").toString()));
        visitor.setSaw(true);
        visitorService.unLockVistors(visitor,tokenEntity.getId());
        return RespGenerator.returnOK("解锁成功");
    }

    /**
     * 解锁访客
     * @param tokenEntity
     * @return
     */
    @TokenCheck(roles = "user")
    @DeleteMapping("/del/{id}")
    public BaseResponse delVisitor(@LoginUser TokenEntity tokenEntity,@PathVariable Long id){
        if (id==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        visitorService.deleteVisitor(id,tokenEntity.getId());
        return RespGenerator.returnOK("删除成功");
    }

}
